ige.service('AccountService', ['$mdDialog', '$http', '$cookieStore', function ($mdDialog, $http, $cookieStore) {
    var showErrorDlg = function (title, content) {
        $mdDialog.show(
            $mdDialog.alert()
                .clickOutsideToClose(true)
                .title(title)
                .textContent(content)
                .ok('确定')
            //.openFrom('#login')
            //.closeTo('#login')
        );
    };
    var headers = {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        Authorization: $cookieStore.get('access_token')
    };
    var loginApi = ige.systemSetting.apiDomain + 'web/remoteapi/login';

    var getApi = ige.systemSetting.apiDomain + 'web/user/list';
    var deleteApi = ige.systemSetting.apiDomain + 'web/user/delete';
    var addApi = ige.systemSetting.apiDomain + 'web/user/add';
    var editApi = ige.systemSetting.apiDomain + 'web/user/update';

    var result = {
        login: function (userName, password, rem, successCallback) {
            var promise = $http.post(loginApi,
                {name: userName, password: password, rememberme: rem}
                , {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback(response.data)
                } else {
                    showErrorDlg('登陆产生错误', response.data.message);
                }
            }, function (response) {
                showErrorDlg('登陆产生错误', response.statusText);
            });

            return promise;
        },
        getUsers: function (pageSkip, pageSize, filter, successCallback) {
            var promise = $http.post(getApi,
                {
                    pageSkip: pageSkip,
                    pageSize: pageSize,
                    filter: filter
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback(response.data.data)
                } else {
                    showErrorDlg('获取用户出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('获取用户出错', response.statusText);
            });

            return promise;
        },
        getUser: function (id, successCallback) {
            var promise = $http.post(getApi,
                {
                    filter: {
                        id: id
                    }
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback(response.data.data[0])
                } else {
                    showErrorDlg('获取用户出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('获取用户出错', response.statusText);
            });
            return promise;
        },
        deleteUser: function (id, successCallback) {
            var promise = $http.post(deleteApi,
                {id: id}, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('删除出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('删除出错', response.statusText);
            });
            return promise;
        },
        addUser: function (user, successCallback) {
            var promise = $http.post(addApi,
                {
                    username: user.Name,
                    phone: user.Phone,
                    email: user.Email,
                    description: user.Description,
                    password: user.Password
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('添加用户出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('添加用户出错', response.statusText);
            });
            return promise;
        },
        editUser: function (user, successCallback) {
            var promise = $http.post(editApi,
                {
                    id: user.Id,
                    username: user.Name,
                    phone: user.Phone,
                    email: user.Email,
                    description: user.Description,
                    password: user.Password
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('编辑出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('编辑出错', response.statusText);
            });
            return promise;
        }

    };
    return result;
}]);